package com.ffapp.rainmonitorcustom.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.ffapp.rainmonitorcustom.R;

public class WaterBallTextView extends View {

    private float mWaterLevelRatio;
    private float mWaterLevelRatio2;
    Paint mPainText;
    Paint mPainBall;

    private String text1;
    private String text2;

    private String colorleft = "#b7e1fb";
    private String colorright = "#b7e1fb";

    public WaterBallTextView(Context context) {
        super(context);
        init();
    }

    public WaterBallTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaterBallTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public WaterBallTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void setValue(float f1,float f2,String text1 ,String text2,String colorleft,String colorRight){
        mWaterLevelRatio = f1;
        mWaterLevelRatio2 = f2;

        this.text1 = text1;
        this.text2 = text2;

        this.colorleft = colorleft;
        this.colorright = colorRight;


        invalidate();
    }


    void init(){
        mPainText = new Paint();
        mPainText.setColor(Color.parseColor("#b7e1fb"));
        mPainText.setStrokeWidth(3.0f);
        mPainText.setTextSize(getResources().getDimension(R.dimen.base_textsize_small));

        mPainBall = new Paint();
        mPainBall.setColor(Color.parseColor("#b7e1fb"));
        mPainBall.setStrokeWidth(3.0f);
        mPainBall.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawvalueText(canvas);
    }

    void drawvalueText(Canvas canvas){
        int circleRadios = 5;
        int OFFFSET_X = 40;

        if(mWaterLevelRatio == 0 && mWaterLevelRatio2 == 0){
            return;
        }
        Log.i("drawtext","getWidth--->"+getWidth());
        Log.i("drawtext","getHeight--->"+getHeight());
        Log.i("drawtext","canvas--->"+canvas.getWidth());

        float value1StartX = (getWidth() -getHeight())/2+OFFFSET_X;
//        float value1StartY = (getHeight()*(1-((mWaterLevelRatio - mWaterLevelRatio2)/2+mWaterLevelRatio2)));
        float value1StartY = (getHeight()*(1-mWaterLevelRatio/2));
        float value1EndX1 = (getWidth() -getHeight())/2-OFFFSET_X;
//        float value1EndY1 = (getHeight()*(1-((mWaterLevelRatio - mWaterLevelRatio2)/2+mWaterLevelRatio2)))-60;
        float value1EndY1 = getHeight()*0.5f;
        float value1EndX2 = value1EndX1-180;
        float value1EndY2 = value1EndY1;


        float value2StartX = (getWidth() -getHeight())/2 +getHeight()-OFFFSET_X*2;
//        float value1StartY = (getHeight()*(1-((mWaterLevelRatio - mWaterLevelRatio2)/2+mWaterLevelRatio2)));
        float value2StartY = (getHeight()*(1-mWaterLevelRatio2/2));
        float value2EndX1 =(getWidth() -getHeight())/2 +getHeight()+OFFFSET_X;
        float value2EndY1 = getHeight()/2;
        float value2EndX2 = value2EndX1+180;
        float value2EndY2 = value2EndY1;

        if(!TextUtils.isEmpty(colorleft)){
            mPainText.setColor(Color.parseColor(colorleft));
            mPainBall.setColor(Color.parseColor(colorleft));
        }

        canvas.drawLine(value1StartX,value1StartY,value1EndX1,value1EndY1,mPainText);
        canvas.drawLine(value1EndX1,value1EndY1,value1EndX2,value1EndY2,mPainText);

        canvas.drawCircle(value1EndX2-circleRadios,value1EndY2,circleRadios,mPainBall);

//        Paint.FontMetrics fontMetrics = mPainText.getFontMetrics();
        float textHeight = mPainText.getFontMetrics().descent - mPainText.getFontMetrics().ascent;
        float bottomline = mPainText.getFontMetrics().bottom;
        canvas.drawText("核定生态流量",value1EndX2,value1EndY2-bottomline,mPainText);
        canvas.drawText(text1+"",value1EndX2,value1EndY2-bottomline-textHeight,mPainText);



        if(!TextUtils.isEmpty(colorright)){
            mPainText.setColor(Color.parseColor(colorright));
            mPainBall.setColor(Color.parseColor(colorright));
        }


        canvas.drawLine(value2StartX,value2StartY,value2EndX1,value2EndY1,mPainText);
        canvas.drawLine(value2EndX1,value2EndY1,value2EndX2,value2EndY2,mPainText);

        canvas.drawCircle(value2EndX2+circleRadios,value2EndY2,circleRadios,mPainBall);

        canvas.drawText("瞬时总流量",value2EndX1,value2EndY2-bottomline,mPainText);
        canvas.drawText(text2+"",value2EndX1,value2EndY2-bottomline-textHeight,mPainText);

    }

    public void setmWaterLevelRatio(float mWaterLevelRatio) {
        this.mWaterLevelRatio = mWaterLevelRatio;
        invalidate();
    }

    public void setmWaterLevelRatio2(float mWaterLevelRatio2) {
        this.mWaterLevelRatio2 = mWaterLevelRatio2;
        invalidate();
    }
}
